package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.LockMode;
import org.hibernate.Query;

import cn.org.timespace.easyframe.action.permission.EFRoleModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfRoleDAO;
import cn.org.timespace.easyframe.model.permission.EfRole;

/**
 * A data access object (DAO) providing persistence and search support for
 * EfRole entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.org.timespace.easyframe.model.permission.EfRole
 * @author MyEclipse Persistence Tools
 */

public class EfRoleDAOImpl extends EfBaseDAO implements EfRoleDAO {
	// property constants 
	public static final String ROLE_NAME = "roleName";
	public static final String ROLE_CODE = "roleCode";
	public static final String DIC_ROLE_IS_VALID = "dicRoleIsValid";

	public void save(EfRole transientInstance) {
		log.debug("saving EfRole instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EfRole persistentInstance) {
		log.debug("deleting EfRole instance");
		try {
			getSession().close();
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void delete(String roleId) {
		try {
			StringBuffer deleteSql = new StringBuffer("delete from EfRole a where a.roleId ='"+roleId+"' ");
			Query q = getSession().createQuery(deleteSql.toString());
			q.executeUpdate();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	

	public EfRole findById(String roleId) {
		StringBuffer queryString = new StringBuffer("from EfRole a where a.roleId = '"+roleId+"'");
		List<EfRole> resList = getHibernateTemplate().find(queryString.toString());
		if(resList != null && resList.size() == 1){
			return resList.get(0);
		}
		return null;
	}

	public List<EfRole> findByCondition(EFRoleModel model) {
		log.debug("finding all EfRole instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfRole a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getRoleId())){
				queryString.append("and a.roleId = '"+model.getRoleId().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getRoleName())){
				queryString.append("and a.roleName = '"+model.getRoleName().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getRoleCode())){
				queryString.append("and a.roleCode = '"+model.getRoleCode().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicRoleIsValid())){
				queryString.append("and a.dicRoleIsValid like '%"+model.getDicRoleIsValid().trim()+"%' ");
			}
			
			queryString.append(" order by a.roleId desc ");
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EfRole merge(EfRole detachedInstance) {
		log.debug("merging EfRole instance");
		try {
			EfRole result = (EfRole) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(EfRole instance) {
		log.debug("attaching dirty EfRole instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(EfRole instance) {
		log.debug("attaching clean EfRole instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public void update(EfRole transientInstance){
		this.getHibernateTemplate().update(transientInstance);
	}
}